package com.xyd.Servlet;

import com.xyd.Biz.CommodityBiz;
import com.xyd.Biz.Impl.CommodityBizImpl;
import com.xyd.Entity.Commodity;
import com.xyd.Util.JsonServlet;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@WebServlet("/selYSHServlet")
public class selYSHServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        CommodityBiz commodityBiz = new CommodityBizImpl();

        Cookie[] cookies = request.getCookies();
        Cookie cookie1 =null;
        Cookie cookie2 =null;
        for (Cookie c :cookies){
            if(c.getName().equals("YSH1")){
                cookie1 = c;
            }else if(c.getName().equals("YSH2")){
                cookie2 = c;
            }
        }
        List<Commodity> list = new ArrayList<>();
        if(cookie1!=null){
            int id = Integer.parseInt(cookie1.getValue());
            Commodity commodity = commodityBiz.SerachCommodityById(id);
            list.add(commodity);
        }
        if(cookie2!=null){
            int id = Integer.parseInt(cookie2.getValue());
            Commodity commodity = commodityBiz.SerachCommodityById(id);
            list.add(commodity);
        }
        JsonServlet.getJson(request,response,list);
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }
}
